const { Sequelize, DataTypes } = require("sequelize");
const defineModels = (sequelize) => {
    const User = sequelize.define("User", {
        // 默认id自增主键索引
        id: {
            type: DataTypes.INTEGER,
            primaryKey: true,
            autoIncrement: true,
        },
        username: {
            type: DataTypes.STRING,
            allowNull: false,
        },
        password: {
            type: DataTypes.STRING,
            allowNull: false,
        },
    });
    const UserInfo = sequelize.define("UserInfo", {
        id: {
            type: DataTypes.INTEGER,
            primaryKey: true,
            autoIncrement: true,
        },
        // 由于头像转base64过大，所以不使用 char / varchar 等字符串，而使用 text 长文本类型
        avatar: {
            type: DataTypes.TEXT,
            allowNull: false,
        },
        nickname: {
            type: DataTypes.STRING,
            allowNull: false,
        },
        email: {
            type: DataTypes.STRING,
            allowNull: false,
        },
        age: {
            type: DataTypes.INTEGER,
            allowNull: false,
            defaultValue: 0,
        },
        desc: {
            type: DataTypes.STRING,
            allowNull: false,
        },
        gender: {
            type: DataTypes.INTEGER,
            allowNull: false,
        },
    });
    // User 表包含 UserInfo
    User.hasOne(UserInfo, {
        // 设置主从级联关系，即删除主表数据，从表自动删除相关数据
        onDelete: "CASCADE",
        onUpdate: "CASCADE",
    });
    // UserInfo 表 属于 User
    UserInfo.belongsTo(User);
    // 结果是，外键在 UserInfo 上

    const Area = sequelize.define("Area", {
        code: {
            type: DataTypes.STRING,
            primaryKey: true,
        },
        value: {
            type: DataTypes.STRING,
            allowNull: false,
        },
        parentCode: {
            type: DataTypes.STRING,
            allowNull: true,
        },
    });

    const Student = sequelize.define("Student", {
        id: {
            type: DataTypes.INTEGER,
            primaryKey: true,
            autoIncrement: true,
        },
        name: {
            type: DataTypes.STRING,
            allowNull: false,
        },
        age: {
            type: DataTypes.INTEGER,
            allowNull: false,
        },
        gender: {
            type: DataTypes.INTEGER,
            allowNull: false,
        },
        hope_salary: {
            type: DataTypes.INTEGER,
            allowNull: false,
        },
        salary: {
            type: DataTypes.INTEGER,
            allowNull: false,
        },
        classNumber: {
            type: DataTypes.STRING,
            allowNull: false,
        },
        province: {
            type: DataTypes.STRING,
            allowNull: false,
        },
        city: {
            type: DataTypes.STRING,
            allowNull: false,
        },
        area: {
            type: DataTypes.STRING,
            allowNull: false,
        },
    });
    return { User, UserInfo, Area, Student };
};

module.exports = defineModels;
